import car_grey_icon from '@/assets/car/car_grey_icon.png'
import car_green_icon from '@/assets/car/car_green_icon.png'
import car_blue_icon from '@/assets/car/car_blue_icon.png'
import camera_grey_icon from '@/assets/car/camera_grey_icon.png'
import { ElTable } from 'element-plus'

/**
 * 获取节点的icon
 * @param status offline：离线 online：在线 2：运动
 * @param type camera：摄像头 null: 车
 */
export function getNodeTypeIcon(status: string, type: string) {
  const iconTypeArr = [[car_grey_icon, car_green_icon, car_blue_icon], [camera_grey_icon]]
  if (type === 'camera') {
    return { background: `url(${iconTypeArr[1][0]})` }
  } else {
    return { background: `url(${iconTypeArr[0][_statusFormat(status)]})` }
  }
}

function _statusFormat(str: string): number {
  switch (str) {
    case 'offline':
      return 0
    case 'online':
      return 1
    default:
      return 0
  }
}

/**
 * 设置指定的表格的滚动高度到当前选中行
 * @param tableRef
 * @param checkedData
 * @param imei
 */
export function scrollToColumn(tableRef: InstanceType<typeof ElTable>, checkedData: any[], imei: string) {
  if (!imei) return
  try {
    const rowHeightList = []
    // @ts-ignore
    let temp: HTMLElement[] = tableRef.$el.getElementsByClassName('el-table__row') //获取到的都是元素
    for (let i = 0; i < temp.length; i++) {
      const item = temp[i]
      // @ts-ignore
      rowHeightList.push(item.scrollHeight)
    }
    let rowIndex = 0 //选中行位于第几行
    for (let i = 0; i < checkedData.length; i++) {
      if (checkedData[i].imei == imei) {
        rowIndex = i
        break
      }
    }
    let totalHeight = 0 //求出选中行之前的的高度之和,需要注意的是,当前行的高度不能包含进去
    for (let index = 0; index < rowHeightList.length; index++) {
      const row = rowHeightList[index]
      if (index < rowIndex) {
        totalHeight += row
      }
    }
    // 滚动到指定行
    tableRef.setScrollTop(totalHeight)
  } catch (error) {
    console.error('错误', error)
  }
}

/**
 * 解析nodeId
 * @param nodeId
 */
export function resolveNodeId(nodeId: string){
  const arr = nodeId.split("-")
  return {
    type: arr[0],
    id: arr[1],
    channel: arr[2],
    idChannel: arr[1] + "-" + arr[2]
  }
}
